var utils = require("../lib/Utils");

exports.login = function (req,res,next) {
    utils.render(req,res,"login",{});
}

exports.valid = function (req,res,next) {
    utils.httpPost("/user/validUser",{username:req.body.username,passwd:req.body.passwd},function (data) {
        if(data.code === "0"){
            var user = data.data;
            req.session.userid = user.id;
            req.session.userName = user.userName;
            req.session.suppId = user.suppId;
            req.session.state = user.state;
            res.status(200).end(JSON.stringify({code:"0",msg:"验证成功",url:utils.menus(req,"main")}));
        }else {
            res.status(200).end(JSON.stringify({code:"-1",msg:"验证失败"}));
        }
    });

}

exports.register = function (req,res,next) {
    utils.render(req,res,"register",{});
}

exports.forgotpass=function (req,res,next) {
    utils.render(req,res,"forgotpass",{});
}


exports.registerUser = function (req,res,next) {
    var param = {
        username:req.body.username,
        suppName:req.body.company,
        passwd:req.body.passwd
    }


    utils.httpPost("/user/register",param,function (data) {
        if(data.code === "0"){
            res.status(200).end(JSON.stringify({code:"0",msg:"注册成功，请等待审核",url:utils.menus(req,"login")}));
        }else {
            res.status(200).end(JSON.stringify({code:"-1",msg:"注册失败"}));
        }
    });
}

exports.logout = function (req,res,next) {
    req.session.userid = null;
    req.session.userName = null;
    req.session.suppId = null;
    req.session.state = null;

    utils.render(req,res,"login",{});
}